local num = tonumber(ARGV[1])
local lockKey = KEYS[1]
local stockKey = KEYS[2]

local lockExist = redis.call("GET", lockKey)
if (lockExist) then
    return -2
end
local balance = redis.call("DECRBY", stockKey, num)
if (balance < 0) then
    redis.call("SET", lockKey, '1')
    redis.call("SET", stockKey, 0)
    return -1
else
    return balance;
end